from PySide6.QtGui import QIcon
from PySide6.QtWidgets import *
from qfluentwidgets import FlowLayout
from qfluentwidgets.window.stacked_widget import StackedWidget

from components import AppCard
from .pdf_view import PdfToWordView, PdfEnOrDeCryptView, TxtToPdfView, MergeToPdfView, PdfToImageView
from resource import OfficeIcon


class PdfHomeFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = FlowLayout(self, needAni=True)

        self.to_word = AppCard(OfficeIcon.WORD2, 'PDF 转 Word', 'PDF 批量转 Word')
        self.enOrDeCrypt = AppCard(OfficeIcon.DATA1, 'PDF加密/解密', '对PDF批量加密/解密')
        self.txtToPdf = AppCard(OfficeIcon.TXT, 'Txt转PDF', 'Txt批量转PDF')
        self.mergeToPdf = AppCard(OfficeIcon.PDF5, '合并PDF', '将多个PDF合成一个PDF')
        self.pdfToImage = AppCard(OfficeIcon.IMG, 'PDF转图片', 'PDF批量转图片')

        self.layout.addWidget(self.to_word)
        self.layout.addWidget(self.enOrDeCrypt)
        self.layout.addWidget(self.txtToPdf)
        self.layout.addWidget(self.mergeToPdf)
        self.layout.addWidget(self.pdfToImage)


class PdfInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("PdfInterface")
        self.stackWidget = QStackedWidget(self)

        self.initUI()
        self.initLayout()
        self.initButtonEvent()

    def initUI(self):
        self.homeFrame = PdfHomeFrame()
        self.to_word = PdfToWordView()
        self.enOrDeCrypt = PdfEnOrDeCryptView()
        self.txtToPdf = TxtToPdfView()
        self.mergeToPdf = MergeToPdfView()
        self.pdfToImage = PdfToImageView()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.to_word)
        self.stackWidget.addWidget(self.enOrDeCrypt)
        self.stackWidget.addWidget(self.txtToPdf)
        self.stackWidget.addWidget(self.mergeToPdf)
        self.stackWidget.addWidget(self.pdfToImage)

        self.stackWidget.setCurrentIndex(0)

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 40, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def initButtonEvent(self):
        self.homeFrame.to_word.clicked.connect(lambda: self.switchTo(self.to_word))
        self.homeFrame.enOrDeCrypt.clicked.connect(lambda: self.switchTo(self.enOrDeCrypt))
        self.homeFrame.txtToPdf.clicked.connect(lambda: self.switchTo(self.txtToPdf))
        self.homeFrame.mergeToPdf.clicked.connect(lambda: self.switchTo(self.mergeToPdf))
        self.homeFrame.pdfToImage.clicked.connect(lambda: self.switchTo(self.pdfToImage))

        self.to_word.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.enOrDeCrypt.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.txtToPdf.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.mergeToPdf.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.pdfToImage.returned.connect(lambda: self.switchTo(self.homeFrame))

    def switchTo(self, widget):
        self.stackWidget.setCurrentWidget(widget)
